/**
 * Created by wenchuan.li on 2018/6/14 11:24.
 */
define(function () { //编辑，保存

    var self,date,infoItem,
        app = win.app,

        cdata = {
            flag:'0',//标记 新增0 还是修改1
            GWBASEQ: '',
            GWBA007: '',//分类
            GWBA001: '',//事项
            GWBA005: '',//地址
            GWBA002: 'N',//是否全天
            GWBADOCDATE: '',//日期
            GWBA003: '',//开始时间
            GWBA004: '',//结束时间
            GWBA009: '0',//重复
            GWBA010: '0',//结束重复
            GWBA011: '',//结束重复日期
            GWBA012: '0',//提醒
            GWBA014: '1',//显示为
            GWBA006: '',//内容
            CUSTOMER: '',//拜访客户名字
            GWBA015: '',//拜访客户编号
            GWBA016: '',//拜访人员姓名
            GWBA017: '',//职位编号
            GWBA018: ''//角色编号

        },
        g_js = [], //角色
        g_zw = [] //职位
    ;

    var Xsledit = app.Module.extend({

        init: function () {
            self = this;
            infoItem = app.pageParameters['gwat151-Taskinfo'].item;
            if (infoItem) {

                cdata.GWBAENT = infoItem.GWBAENT,
                cdata.GWBAID = infoItem.GWBAID,
                cdata.GWBASEQ = infoItem.GWBASEQ,
                cdata.GWBA007 = infoItem.GWBA007,//分类
                cdata.GWBA001 = infoItem.GWBA001,//事项
                cdata.GWBA005 = infoItem.GWBA005,//地址
                cdata.GWBA002 = infoItem.GWBA002,//是否全天
                cdata.GWBADOCDATE = infoItem.GWBADOCDATE,//日期
                cdata.GWBA003 = infoItem.GWBA003.replace(' ','T'),//开始时间
                cdata.GWBA004 = infoItem.GWBA004.replace(' ','T'),//结束时间
                cdata.GWBA009 = infoItem.GWBA009,//重复
                cdata.GWBA010 = infoItem.GWBA010,//结束重复
                cdata.GWBA011 = infoItem.GWBA011,//结束重复日期
                cdata.GWBA012 = infoItem.GWBA012,//提醒
                cdata.GWBA014 = infoItem.GWBA014,//显示为
                cdata.GWBA006 = infoItem.GWBA006,//内容
                cdata.CUSTOMER = infoItem.GWAKL004,//拜访客户 名字
                cdata.GWBA015 = infoItem.GWBA015,//拜访客户编号
                cdata.GWBA016 = infoItem.GWBA016,//拜访人员姓名
                cdata.GWBA017 = infoItem.GWBA017,//职位编号
                cdata.GWBA018 = infoItem.GWBA018//角色编号
            }
            self._super({
                _readyBefore: function () {
                    var mainjs = app.pageModule('p_main');

                    if (!mainjs) {
                        mainjs = app.pageModule('p_businessman');
                        if (!mainjs){
                            mainjs = app.pageModule('p_after_sale');
                            if (!mainjs){
                                mainjs = app.pageModule('p_outer-user');
                            }
                        }
                    }
                    g_js = mainjs.juese;
                    g_zw = mainjs.zhiwei;

                }
            }, win);


        },
        updateData: function () {

            app.ajax({
                xurl: "api/wx/gwat151/gwat151Update",
                loading: true,
                before: function () {
                    if (cdata.GWBA007 == '') {
                        return '分类不能为空';
                    }
                    if (cdata.GWBA001 == '') {
                        return '事项不能为空';
                    }
                    if (cdata.GWBADOCDATE == '') {
                        return '日期不能为空';
                    }
                    if (cdata.GWBA003 == '') {
                        return '开始时间不能为空';
                    }
                    if (cdata.GWBA004 == '') {
                        return '结束时间不能为空';
                    }
                    if (cdata.GWBA009 == '0') {
                        cdata.GWBA010 = '';
                        cdata.GWBA011 = '';
                    }
                    if (cdata.GWBA010 == '0') {
                        cdata.GWBA011 = '';
                    }
                },
                xdata: cdata,
                confirm: {
                    msg: '是否提交信息？'
                },
                cb: function (result) {
                    if (result.ok) {
                        app.alert({
                            msg: '修改成功！',
                            ok: function () {
                                app.pageParameters['gwat151-Taskinfo'].cb();
                                app.back(1);

                            }
                        });
                    }
                }
            });
        },
        deleteData: function () {

            //重新给key 赋值 防止因进入编辑页面 修改了标题而无法删除
            cdata.GWBAENT = infoItem.GWBAENT,
            cdata.GWBAID = infoItem.GWBAID,
            cdata.GWBASEQ = infoItem.GWBASEQ,
            cdata.GWBA001 = infoItem.GWBA001,//事项

            app.ajax({
                xurl: "api/wx/gwat151/gwat151Delete",
                loading: true,
                xdata: cdata,
                confirm: {
                    msg: '是否删除信息？'
                },
                cb: function (result) {
                    if (result.ok) {
                        app.alert({
                            msg: '删除成功！',
                            ok: function () {
                                cdata=[];
                                app.pageParameters['gwat151-Taskinfo'].cb();
                                app.back(1);
                            }
                        });
                    }
                }
            });
        },
        _lazy: function () {
            this._super();
        },
        _ready: function () {
            new Vue({
                el: app.getByid(doc, 'gwat151Info'),
                data: {
                    sub: cdata,
                    g_js: g_js,
                    g_zw: g_zw
                },
                methods: {
                    GWBA002: function () {
                        cdata.GWBA003 = cdata.GWBADOCDATE + "T09:00:00"
                        cdata.GWBA004 = cdata.GWBADOCDATE + "T21:00:00"
                    },
                    showMap: function () {
                        app.openPage({
                            src: "../../html/wx/map.html",
                            argsKey: 'map_page', // 设置页面请求参数key值
                            args: {
                                cb: function (result) {
                                    // 更新数据
                                    cdata.GWBA005 = result.address;
                                }
                            }
                        });
                    },
                    showCustomers: function () {

                        app.openPage({
                            src: "gwat151/gwat151-user-info.html",
                            argsKey: 'customersadd_page',
                            args: {
                                cb: function (obj) {

                                    cdata.GWBA015 = obj.GWAK001;
                                    cdata.CUSTOMER = obj.GWAKL004;
                                }
                            }
                        });
                    },
                    updateData:function () {
                        self.updateData();
                    },
                    deleteData:function () {
                        self.deleteData();
                    }
                }
            });

            mui('.mui-scroll-wrapper').scroll({
                deceleration: 0.0005
            });
        }
    });

    new Xsledit();

});